package com.ml.doctor.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ml.doctor.R;
import com.ml.doctor.activity.AccountActivity;
import com.ml.doctor.activity.LoginActivity;
import com.ml.doctor.activity.MyServiceActivity;
import com.ml.doctor.activity.UpdateZhenShuActivity;
import com.ml.doctor.activity.UserInfoActivity;
import com.ml.doctor.bean.UserInfoBean;
import com.ml.doctor.network.NetworkApi;
import com.ml.doctor.network.NetworkManager;
import com.ml.doctor.utils.LocalShared;
import com.ml.doctor.view.BasicItem;
import com.ml.doctor.view.CycleImageView;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by kun on 17/12/1.
 */

public class MyFragment extends Fragment {


    @BindView(R.id.iv_avatar)
    CycleImageView ivAvatar;
    @BindView(R.id.txt_name)
    TextView txtName;
    @BindView(R.id.txt_status)
    TextView txtStatus;
    @BindView(R.id.txt_jj)
    TextView txtJj;
    @BindView(R.id.tv_loginOut)
    TextView loginoutTv;
    @BindView(R.id.bi_renz)
    BasicItem biRenz;
    @BindView(R.id.iv_switch)
    ImageView ivSwitch;

    UserInfoBean userInfoBean;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my, container, false);
        ButterKnife.bind(this, view);
        NetworkApi.getUserInfo(listener);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        Glide.with(getActivity()).load(LocalShared.getInstance(getActivity()).getPhoto()).asBitmap().into(ivAvatar);
        txtName.setText(LocalShared.getInstance(getActivity()).getUserNick());
        txtJj.setText(LocalShared.getInstance(getActivity()).getPro());
    }

    public NetworkManager.SuccessCallback<UserInfoBean> listener = new NetworkManager.SuccessCallback<UserInfoBean>() {
        @Override
        public void onSuccess(UserInfoBean response) {
            if (response == null) {
                return;
            }
            userInfoBean = response;
            Glide.with(getActivity()).load(response.docter_photo).asBitmap().into(ivAvatar);
            txtName.setText(response.doctername);
            txtJj.setText(response.pro);
            LocalShared.getInstance(getActivity()).setPhoto(response.docter_photo);
            LocalShared.getInstance(getActivity()).setUserPro(response.pro);
            if (response.online_status == 1) {
                ivSwitch.setBackgroundResource(R.drawable.icon_switch_open);
            } else {
                ivSwitch.setBackgroundResource(R.drawable.icon_switch_close);
            }
            if (response.zige.equals("已通过审核")) {
                txtStatus.setVisibility(View.GONE);
                biRenz.setVisibility(View.GONE);
            } else {
                txtStatus.setText("未审核");
                txtStatus.setTextColor(Color.parseColor("#9b9b9b"));
                txtStatus.setBackgroundResource(R.drawable.shap_bg_gray);
            }
        }
    };


    @OnClick({R.id.bi_renz, R.id.bi_wdzl, R.id.bi_wdzh, R.id.bi_wdfw, R.id.tv_loginOut, R.id.iv_switch})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bi_renz:
                startActivity(new Intent(getActivity(), UpdateZhenShuActivity.class));
                break;
            case R.id.bi_wdzl:
                startActivity(new Intent(getActivity(), UserInfoActivity.class));
                break;
            case R.id.bi_wdzh:
                startActivity(new Intent(getActivity(), AccountActivity.class));
                break;
            case R.id.bi_wdfw:
                startActivity(new Intent(getActivity(), MyServiceActivity.class));
                break;
            case R.id.tv_loginOut:
                LocalShared.getInstance(getActivity()).loginOut();
                startActivity(new Intent(getActivity(), LoginActivity.class));
                getActivity().finish();
                break;
            case R.id.iv_switch:
                switchStatus(userInfoBean.online_status == 1 ? 2 : 1);
                break;
        }
    }

    public void switchStatus(int status)

    {
        Map<String, String> map = new HashMap<>();
        map.put("docterid", LocalShared.getInstance(getActivity()).getUserId() + "");
        map.put("online_status", status + "");
        NetworkManager.getInstance().getResultClass(NetworkApi.BasicUrl + "docter/update_online_status", map, Object.class, new NetworkManager.SuccessCallback() {
            @Override
            public void onSuccess(Object response) {
                NetworkApi.getUserInfo(listener);
            }
        }, null);
    }
}
